package beusoft.ccjk.com.proxy;

import android.app.Instrumentation;
import android.content.Context;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by CCJK on 2017/12/3.
 */

public class Util {

    public static void bind(Context context) {
        try {
            Class clazz = Class.forName("android.app.ActivityThread");
            Method method = clazz.getMethod("currentActivityThread", new Class[]{});
            method.setAccessible(true);
            Object activityThread = method.invoke(null, new Object[]{});

            //
            Field field = clazz.getDeclaredField("mInstrumentation");
            field.setAccessible(true);
            Instrumentation mInstrumentation = (Instrumentation) field.get(activityThread);
            InstrumentationProxy proxy=new InstrumentationProxy(mInstrumentation);
            field.set(activityThread,proxy);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

    }

    public static void test(){
        try {
            Class clazz = Class.forName("android.app.ActivityThread");
            Method method = clazz.getMethod("currentActivityThread", new Class[]{});
            method.setAccessible(true);
            Object activityThread = method.invoke(null, new Object[]{});

            //
            Field field = clazz.getDeclaredField("mInstrumentation");
            field.setAccessible(true);
            Instrumentation mInstrumentation = (Instrumentation) field.get(activityThread);
        }catch (Exception e){

        }

    }
}
